﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Linq;

using DbHelper;//自定义帮助类
using Gx.Model.Entity;//实体
using Gx.Common;//通用类
namespace Gx.Dal.Employee
{
    /// <summary>
    /// 采购单
    /// </summary>
    public partial class G_PurchaseOrderInfoDal : SqlServerHelper, IDisposable
    {
        public int AddG_PurchaseOrderInfo(G_PurchaseOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("INSERT INTO G_PurchaseOrderInfo");
            strSql.Append("(");
            strSql.Append("BillNo,");
            strSql.Append("BillStatus,");
            strSql.Append("BillImageUrl,");
            strSql.Append("BusinessCompanyId,");
            strSql.Append("BusinessCompanyName,");
            strSql.Append("IsStockIn,");
            strSql.Append("ConfirmRemark,");
            strSql.Append("TotalPrice,");
            strSql.Append("AccountPaid,");
            strSql.Append("GoodsId,");
            strSql.Append("GoodsName,");
            strSql.Append("EmployeeId,");
            strSql.Append("CheckStatus,");
            strSql.Append("CheckUser,");
            strSql.Append("CheckDate,");
            strSql.Append("CheckUserHistory,");
            strSql.Append("Remark,");
            strSql.Append("Status,");
            strSql.Append("AddUser,");
            strSql.Append("AddTime,");
            strSql.Append("LastEditUser,");
            strSql.Append("LastEditTime");
            strSql.Append(")");
            strSql.Append("VALUES");
            strSql.Append("(");
            strSql.Append("@BillNo,");
            strSql.Append("@BillStatus,");
            strSql.Append("@BillImageUrl,");
            strSql.Append("@BusinessCompanyId,");
            strSql.Append("@BusinessCompanyName,");
            strSql.Append("@IsStockIn,");
            strSql.Append("@ConfirmRemark,");
            strSql.Append("@TotalPrice,");
            strSql.Append("@AccountPaid,");
            strSql.Append("@GoodsId,");
            strSql.Append("@GoodsName,");
            strSql.Append("@EmployeeId,");
            strSql.Append("@CheckStatus,");
            strSql.Append("@CheckUser,");
            strSql.Append("@CheckDate,");
            strSql.Append("@CheckUserHistory,");
            strSql.Append("@Remark,");
            strSql.Append("@Status,");
            strSql.Append("@AddUser,");
            strSql.Append("@AddTime,");
            strSql.Append("@LastEditUser,");
            strSql.Append("@LastEditTime");
            strSql.Append(");SELECT @@IDENTITY;");
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@BillNo", SqlDbType.NVarChar, 30) { Value = model.BillNo },
                new SqlParameter("@BillStatus", SqlDbType.Int, 4) { Value = model.BillStatus },
                new SqlParameter("@BillImageUrl", SqlDbType.NVarChar, 100) { Value = model.BillImageUrl },
                new SqlParameter("@BusinessCompanyId", SqlDbType.Int, 4) { Value = model.BusinessCompanyId },
                new SqlParameter("@BusinessCompanyName", SqlDbType.NVarChar, 200) { Value = model.BusinessCompanyName },
                new SqlParameter("@IsStockIn", SqlDbType.Bit) { Value = model.IsStockIn },
                new SqlParameter("@ConfirmRemark", SqlDbType.NVarChar, 200) { Value = model.ConfirmRemark },
                new SqlParameter("@TotalPrice", SqlDbType.Decimal, 9) { Value = model.TotalPrice },
                new SqlParameter("@AccountPaid", SqlDbType.Decimal, 9) { Value = model.AccountPaid },
                new SqlParameter("@GoodsId", SqlDbType.NVarChar, 200) { Value = model.GoodsId },
                new SqlParameter("@GoodsName", SqlDbType.NVarChar, 200) { Value = model.GoodsName },
                new SqlParameter("@EmployeeId", SqlDbType.Int, 4) { Value = model.EmployeeId },
                new SqlParameter("@CheckStatus", SqlDbType.Int, 4) { Value = model.CheckStatus },
                new SqlParameter("@CheckUser", SqlDbType.Int, 4) { Value = model.CheckUser },
                new SqlParameter("@CheckDate", SqlDbType.DateTime) { Value = model.CheckDate },
                new SqlParameter("@CheckUserHistory", SqlDbType.NVarChar, 100) { Value = model.CheckUserHistory },
                new SqlParameter("@Remark", SqlDbType.NVarChar, 200) { Value = model.Remark },
                new SqlParameter("@Status", SqlDbType.Int, 4) { Value = model.Status },
                new SqlParameter("@AddUser", SqlDbType.Int, 4) { Value = model.AddUser },
                new SqlParameter("@AddTime", SqlDbType.DateTime) { Value = model.AddTime },
                new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
                new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
            };
            return ExecuteScalar(ConfigHelper.ConnectionStrings, strSql.ToString(), pList.ToArray()).IntParse();
        }


        //public string AddG_PurchaseOrderInfo(G_PurchaseOrderInfo model)
        //{
        //    StringBuilder strSql = new StringBuilder();
        //    strSql.Append("INSERT INTO G_PurchaseOrderInfo");
        //    strSql.Append("(");
        //    strSql.Append("BillNo,");
        //    strSql.Append("BillStatus,");
        //    strSql.Append("BillImageUrl,");
        //    strSql.Append("BusinessCompanyId,");
        //    strSql.Append("BusinessCompanyName,");
        //    strSql.Append("IsStockIn,");
        //    strSql.Append("ConfirmRemark,");
        //    strSql.Append("TotalPrice,");
        //    strSql.Append("AccountPaid,");
        //    strSql.Append("GoodsId,");
        //    strSql.Append("GoodsName,");
        //    strSql.Append("EmployeeId,");
        //    strSql.Append("CheckStatus,");
        //    strSql.Append("CheckUser,");
        //    strSql.Append("CheckDate,");
        //    strSql.Append("CheckUserHistory,");
        //    strSql.Append("Remark,");
        //    strSql.Append("Status,");
        //    strSql.Append("AddUser,");
        //    strSql.Append("AddTime,");
        //    strSql.Append("LastEditUser,");
        //    strSql.Append("LastEditTime");
        //    strSql.Append(")");
        //    strSql.Append("VALUES");
        //    strSql.Append("(");
        //    strSql.Append("@BillNo,");
        //    strSql.Append("@BillStatus,");
        //    strSql.Append("@BillImageUrl,");
        //    strSql.Append("@BusinessCompanyId,");
        //    strSql.Append("@BusinessCompanyName,");
        //    strSql.Append("@IsStockIn,");
        //    strSql.Append("@ConfirmRemark,");
        //    strSql.Append("@TotalPrice,");
        //    strSql.Append("@AccountPaid,");
        //    strSql.Append("@GoodsId,");
        //    strSql.Append("@GoodsName,");
        //    strSql.Append("@EmployeeId,");
        //    strSql.Append("@CheckStatus,");
        //    strSql.Append("@CheckUser,");
        //    strSql.Append("@CheckDate,");
        //    strSql.Append("@CheckUserHistory,");
        //    strSql.Append("@Remark,");
        //    strSql.Append("@Status,");
        //    strSql.Append("@AddUser,");
        //    strSql.Append("@AddTime,");
        //    strSql.Append("@LastEditUser,");
        //    strSql.Append("@LastEditTime");
        //    strSql.Append(");SELECT @@IDENTITY;");
        //    List<SqlParameter> pList = new List<SqlParameter>
        //    {
        //        new SqlParameter("@BillNo", SqlDbType.NVarChar, 30) { Value = model.BillNo },
        //        new SqlParameter("@BillStatus", SqlDbType.Int, 4) { Value = model.BillStatus },
        //        new SqlParameter("@BillImageUrl", SqlDbType.NVarChar, 100) { Value = model.BillImageUrl },
        //        new SqlParameter("@BusinessCompanyId", SqlDbType.Int, 4) { Value = model.BusinessCompanyId },
        //        new SqlParameter("@BusinessCompanyName", SqlDbType.NVarChar, 200) { Value = model.BusinessCompanyName },
        //        new SqlParameter("@IsStockIn", SqlDbType.Bit) { Value = model.IsStockIn },
        //        new SqlParameter("@ConfirmRemark", SqlDbType.NVarChar, 200) { Value = model.ConfirmRemark },
        //        new SqlParameter("@TotalPrice", SqlDbType.Decimal, 9) { Value = model.TotalPrice },
        //        new SqlParameter("@AccountPaid", SqlDbType.Decimal, 9) { Value = model.AccountPaid },
        //        new SqlParameter("@GoodsId", SqlDbType.NVarChar, 200) { Value = model.GoodsId },
        //        new SqlParameter("@GoodsName", SqlDbType.NVarChar, 200) { Value = model.GoodsName },
        //        new SqlParameter("@EmployeeId", SqlDbType.Int, 4) { Value = model.EmployeeId },
        //        new SqlParameter("@CheckStatus", SqlDbType.Int, 4) { Value = model.CheckStatus },
        //        new SqlParameter("@CheckUser", SqlDbType.Int, 4) { Value = model.CheckUser },
        //        new SqlParameter("@CheckDate", SqlDbType.DateTime) { Value = model.CheckDate },
        //        new SqlParameter("@CheckUserHistory", SqlDbType.NVarChar, 100) { Value = model.CheckUserHistory },
        //        new SqlParameter("@Remark", SqlDbType.NVarChar, 200) { Value = model.Remark },
        //        new SqlParameter("@Status", SqlDbType.Int, 4) { Value = model.Status },
        //        new SqlParameter("@AddUser", SqlDbType.Int, 4) { Value = model.AddUser },
        //        new SqlParameter("@AddTime", SqlDbType.DateTime) { Value = model.AddTime },
        //        new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
        //        new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
        //    };
        //    using (SqlConnection conn = new SqlConnection(ConfigHelper.ConnectionStrings))
        //    {
        //        conn.Open();
        //        using (SqlCommand cmd = new SqlCommand(strSql.ToString(), conn))
        //        {
        //            SqlTransaction trans = conn.BeginTransaction();
        //            cmd.Parameters.AddRange(pList.ToArray());
        //            cmd.Transaction = trans;
        //            int PurchaseOrderId = cmd.ExecuteScalar().IntParse();
        //            if (PurchaseOrderId == 0)
        //            {
        //                trans.Rollback();
        //                return "服务端出现错误,请联系系统管理员";
        //            }
        //            try
        //            {
        //                foreach (var item in model.GoodsList)
        //                {
        //                    strSql.Append("INSERT INTO G_StockInGoodsInfo");
        //                    strSql.Append("(");
        //                    strSql.Append("PurchaseOrderId,");
        //                    strSql.Append("GoodsId,");
        //                    strSql.Append("NeedNum,");
        //                    strSql.Append("UnitPrice,");
        //                    strSql.Append("TotalPrice,");
        //                    strSql.Append("Remart,");
        //                    strSql.Append("Status,");
        //                    strSql.Append("AddUser,");
        //                    strSql.Append("AddTime,");
        //                    strSql.Append("LastEditUser,");
        //                    strSql.Append("LastEditTime");
        //                    strSql.Append(")");
        //                    strSql.Append("VALUES");
        //                    strSql.Append("(");
        //                    strSql.Append("@PurchaseOrderId,");
        //                    strSql.Append("@GoodsId,");
        //                    strSql.Append("@NeedNum,");
        //                    strSql.Append("@UnitPrice,");
        //                    strSql.Append("@TotalPrice,");
        //                    strSql.Append("@Remart,");
        //                    strSql.Append("@Status,");
        //                    strSql.Append("@AddUser,");
        //                    strSql.Append("@AddTime,");
        //                    strSql.Append("@LastEditUser,");
        //                    strSql.Append("@LastEditTime");
        //                    strSql.Append(")");
        //                  pList = new List<SqlParameter>
        //    {
        //        new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = PurchaseOrderId },
        //        new SqlParameter("@GoodsId", SqlDbType.Int, 4) { Value = item.GoodsId },
        //        new SqlParameter("@NeedNum", SqlDbType.Decimal, 9) { Value = item.NeedNum },
        //        new SqlParameter("@UnitPrice", SqlDbType.Decimal, 9) { Value = item.UnitPrice },
        //        new SqlParameter("@TotalPrice", SqlDbType.Decimal, 9) { Value = item.TotalPrice },
        //        new SqlParameter("@Remart", SqlDbType.NVarChar, 200) { Value = item.Remart },
        //        new SqlParameter("@Status", SqlDbType.Int, 4) { Value = item.Status },
        //        new SqlParameter("@AddUser", SqlDbType.Int, 4) { Value = item.AddUser },
        //        new SqlParameter("@AddTime", SqlDbType.DateTime) { Value = item.AddTime },
        //        new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = item.LastEditUser },
        //        new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = item.LastEditTime }
        //    };
        //                    cmd.CommandText = strSql.ToString();
        //                    cmd.Parameters.AddRange(pList.ToArray());
        //                    cmd.ExecuteNonQuery();
        //                }
        //                if (model.PriceList != null)
        //                {
        //                    foreach (var item in model.PriceList)
        //                    {
        //                        strSql = new StringBuilder();
        //                        strSql.Append("INSERT INTO G_StockInPriceInfo");
        //                        strSql.Append("(");
        //                        strSql.Append("PurchaseOrderId,");
        //                        strSql.Append("Price,");
        //                        strSql.Append("Remart,");
        //                        strSql.Append("Status,");
        //                        strSql.Append("AddUser,");
        //                        strSql.Append("AddTime,");
        //                        strSql.Append("LastEditUser,");
        //                        strSql.Append("LastEditTime");
        //                        strSql.Append(")");
        //                        strSql.Append("VALUES");
        //                        strSql.Append("(");
        //                        strSql.Append("@PurchaseOrderId,");
        //                        strSql.Append("@Price,");
        //                        strSql.Append("@Remart,");
        //                        strSql.Append("@Status,");
        //                        strSql.Append("@AddUser,");
        //                        strSql.Append("@AddTime,");
        //                        strSql.Append("@LastEditUser,");
        //                        strSql.Append("@LastEditTime");
        //                        strSql.Append(")");
        //                        pList = new List<SqlParameter>
        //                {
        //                    new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = PurchaseOrderId },
        //                    new SqlParameter("@Price", SqlDbType.Decimal, 9) { Value = item.Price },
        //                    new SqlParameter("@Remart", SqlDbType.NVarChar, 200) { Value = item.Remart },
        //                    new SqlParameter("@Status", SqlDbType.Int, 4) { Value = item.Status },
        //                    new SqlParameter("@AddUser", SqlDbType.Int, 4) { Value = item.AddUser },
        //                    new SqlParameter("@AddTime", SqlDbType.DateTime) { Value = item.AddTime },
        //                    new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = item.LastEditUser },
        //                    new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = item.LastEditTime }
        //                };
        //                    }
        //                }

        //                trans.Commit();

        //            }
        //            catch (Exception ex)
        //            {
        //                trans.Rollback();
        //                string strEx = ex.ToString();
        //                LogHelper.WriteErrorLog(strEx);
        //                return strEx;
        //            }
        //        }
        //        conn.Close();
        //        return "";
        //    }
        //}

        /// <summary>
        /// 作废单据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool DelG_PurchaseOrderInfo_BillStatus(G_PurchaseOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE G_PurchaseOrderInfo SET ");
            strSql.Append("[BillStatus]=@BillStatus,  ");
            strSql.Append("LastEditUser=@LastEditUser, ");
            strSql.Append("LastEditTime=@LastEditTime ");
            strSql.Append("WHERE PurchaseOrderId=@PurchaseOrderId  ");
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = model.PurchaseOrderId },
                new SqlParameter("@BillStatus", SqlDbType.TinyInt) { Value = model.BillStatus },
                new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
                new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
            };
            return ExecuteNonQuery(ConfigHelper.ConnectionStrings, strSql.ToString(), pList.ToArray()) > 0;
        }

        /// <summary>
        /// 审核单据
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditG_PurchaseOrderInfo_CheckUser(G_PurchaseOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE G_PurchaseOrderInfo SET ");
            strSql.Append("CheckStatus=@CheckStatus,  ");
            strSql.Append("CheckUser=@LastEditUser,  ");
            strSql.Append("LastEditUser=@LastEditUser, ");
            strSql.Append("CheckDate=@LastEditTime, ");
            strSql.Append("LastEditTime=@LastEditTime ");
            strSql.Append("WHERE PurchaseOrderId=@PurchaseOrderId  ");
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = model.PurchaseOrderId },
                new SqlParameter("@CheckStatus", SqlDbType.Int) { Value = model.CheckStatus },
                new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
                new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
            };
            return ExecuteNonQuery(ConfigHelper.ConnectionStrings, strSql.ToString(), pList.ToArray()) > 0;
        }

        public bool EditG_PurchaseOrderInfo(G_PurchaseOrderInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("UPDATE G_PurchaseOrderInfo SET ");
            strSql.Append(" BillImageUrl=@BillImageUrl,");
            strSql.Append(" BusinessCompanyId=@BusinessCompanyId,");
            strSql.Append(" TotalPrice=@TotalPrice,");
            strSql.Append(" AccountPaid=@AccountPaid,");
            strSql.Append(" GoodsId=@GoodsId,");
            strSql.Append(" GoodsName=@GoodsName,");
            strSql.Append(" EmployeeId=@EmployeeId,");
            strSql.Append(" Remark=@Remark,");
            strSql.Append(" LastEditUser=@LastEditUser,");
            strSql.Append(" LastEditTime=@LastEditTime");
            strSql.Append(" WHERE PurchaseOrderId=@PurchaseOrderId AND [Status]=0");
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = model.PurchaseOrderId },
                new SqlParameter("@BillImageUrl", SqlDbType.NVarChar, 100) { Value = model.BillImageUrl },
                new SqlParameter("@BusinessCompanyId", SqlDbType.Int, 4) { Value = model.BusinessCompanyId },
                new SqlParameter("@TotalPrice", SqlDbType.Decimal, 9) { Value = model.TotalPrice },
                new SqlParameter("@AccountPaid", SqlDbType.Decimal, 9) { Value = model.AccountPaid },
                new SqlParameter("@GoodsId", SqlDbType.NVarChar, 200) { Value = model.GoodsId },
                new SqlParameter("@GoodsName", SqlDbType.NVarChar, 200) { Value = model.GoodsName },
                new SqlParameter("@EmployeeId", SqlDbType.Int, 4) { Value = model.EmployeeId },
                new SqlParameter("@Remark", SqlDbType.NVarChar, 200) { Value = model.Remark },
                new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
                new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
            };
            return ExecuteNonQuery(ConfigHelper.ConnectionStrings, strSql.ToString(), pList.ToArray()) > 0;
        }
        public List<G_PurchaseOrderInfo> GetG_PurchaseOrderInfoList(string strWhere)
        {
            List<G_PurchaseOrderInfo> list = new List<G_PurchaseOrderInfo>();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");
            strSql.Append(" PurchaseOrderId");
            strSql.Append(",BillNo");
            strSql.Append(",BillStatus");
            strSql.Append(",BillImageUrl");
            strSql.Append(",BusinessCompanyId");
            strSql.Append(",BusinessCompanyName");
            strSql.Append(",IsStockIn");
            strSql.Append(",ConfirmRemark");
            strSql.Append(",TotalPrice");
            strSql.Append(",AccountPaid");
            strSql.Append(",GoodsId");
            strSql.Append(",GoodsName");
            strSql.Append(",EmployeeId");
            strSql.Append(",CheckStatus");
            strSql.Append(",CheckUser");
            strSql.Append(",CheckDate");
            strSql.Append(",CheckUserHistory");
            strSql.Append(",Remark");
            strSql.Append(",Status");
            strSql.Append(",AddUser");
            strSql.Append(",AddTime");
            strSql.Append(",LastEditUser");
            strSql.Append(",LastEditTime");
            strSql.AppendFormat(" FROM G_PurchaseOrderInfo WITH(NOLOCK) WHERE [Status]=0 {0}", strWhere);
            using (SqlDataReader r = ExecuteReader(ConfigHelper.ConnectionStrings, strSql.ToString()))
            {
                int PurchaseOrderId = r.GetOrdinal("PurchaseOrderId");
                int BillNo = r.GetOrdinal("BillNo");
                int BillStatus = r.GetOrdinal("BillStatus");
                int BillImageUrl = r.GetOrdinal("BillImageUrl");
                int BusinessCompanyId = r.GetOrdinal("BusinessCompanyId");
                int BusinessCompanyName = r.GetOrdinal("BusinessCompanyName");
                int IsStockIn = r.GetOrdinal("IsStockIn");
                int ConfirmRemark = r.GetOrdinal("ConfirmRemark");
                int TotalPrice = r.GetOrdinal("TotalPrice");
                int AccountPaid = r.GetOrdinal("AccountPaid");
                int GoodsId = r.GetOrdinal("GoodsId");
                int GoodsName = r.GetOrdinal("GoodsName");
                int EmployeeId = r.GetOrdinal("EmployeeId");
                int CheckStatus = r.GetOrdinal("CheckStatus");
                int CheckUser = r.GetOrdinal("CheckUser");
                int CheckDate = r.GetOrdinal("CheckDate");
                int CheckUserHistory = r.GetOrdinal("CheckUserHistory");
                int Remark = r.GetOrdinal("Remark");
                int Status = r.GetOrdinal("Status");
                int AddUser = r.GetOrdinal("AddUser");
                int AddTime = r.GetOrdinal("AddTime");
                int LastEditUser = r.GetOrdinal("LastEditUser");
                int LastEditTime = r.GetOrdinal("LastEditTime");
                G_PurchaseOrderInfo model = null;
                while (r.Read())
                {
                    model = new G_PurchaseOrderInfo();
                    model.PurchaseOrderId = r.GetInt32(PurchaseOrderId);
                    model.BillNo = r.GetString(BillNo);
                    model.BillStatus = r.GetInt32(BillStatus);
                    model.BillImageUrl = r.GetString(BillImageUrl);
                    model.BusinessCompanyId = r.GetInt32(BusinessCompanyId);
                    model.BusinessCompanyName = r.GetString(BusinessCompanyName);
                    model.IsStockIn = r.GetBoolean(IsStockIn);
                    model.ConfirmRemark = r.GetString(ConfirmRemark);
                    model.TotalPrice = r.GetDecimal(TotalPrice);
                    model.AccountPaid = r.GetDecimal(AccountPaid);
                    model.GoodsId = r.GetString(GoodsId);
                    model.GoodsName = r.GetString(GoodsName);
                    model.EmployeeId = r.GetInt32(EmployeeId);
                    model.CheckStatus = r.GetInt32(CheckStatus);
                    model.CheckUser = r.GetInt32(CheckUser);
                    model.CheckDate = r.GetDateTime(CheckDate);
                    model.CheckUserHistory = r.GetString(CheckUserHistory);
                    model.Remark = r.GetString(Remark);
                    model.Status = r.GetInt32(Status);
                    model.AddUser = r.GetInt32(AddUser);
                    model.AddTime = r.GetDateTime(AddTime);
                    model.LastEditUser = r.GetInt32(LastEditUser);
                    model.LastEditTime = r.GetDateTime(LastEditTime);
                    list.Add(model);
                }
                r.Close();
            }
            return list;
        }

        public List<G_PurchaseOrderInfo> GetG_PurchaseOrderInfoList(string strWhere, int pageIndex, int pageSize, out int totalCount)
        {
            List<G_PurchaseOrderInfo> list = new List<G_PurchaseOrderInfo>();
            totalCount = GetG_PurchaseOrderInfoCount(strWhere);
            if (totalCount == 0)
            {
                return list;
            }
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT * FROM (");
            strSql.Append("SELECT ROW_NUMBER() OVER(ORDER BY PurchaseOrderId DESC) AS ORDERBYID");
            strSql.Append(",PurchaseOrderId");
            strSql.Append(",BillNo");
            strSql.Append(",BillStatus");
            strSql.Append(",BillImageUrl");
            strSql.Append(",BusinessCompanyId");
            strSql.Append(",BusinessCompanyName");
            strSql.Append(",IsStockIn");
            strSql.Append(",ConfirmRemark");
            strSql.Append(",TotalPrice");
            strSql.Append(",AccountPaid");
            strSql.Append(",GoodsId");
            strSql.Append(",GoodsName");
            strSql.Append(",EmployeeId");
            strSql.Append(",CheckStatus");
            strSql.Append(",CheckUser");
            strSql.Append(",CheckDate");
            strSql.Append(",CheckUserHistory");
            strSql.Append(",Remark");
            strSql.Append(",Status");
            strSql.Append(",AddUser");
            strSql.Append(",AddTime");
            strSql.Append(",LastEditUser");
            strSql.Append(",LastEditTime");
            strSql.AppendFormat(" FROM G_PurchaseOrderInfo WITH(NOLOCK) WHERE [Status]=0 {0}) AS T WHERE T.ORDERBYID  BETWEEN {1} AND {2}", strWhere, pageIndex, pageSize);
            using (SqlDataReader r = ExecuteReader(ConfigHelper.ConnectionStrings, strSql.ToString()))
            {
                int PurchaseOrderId = r.GetOrdinal("PurchaseOrderId");
                int BillNo = r.GetOrdinal("BillNo");
                int BillStatus = r.GetOrdinal("BillStatus");
                int BillImageUrl = r.GetOrdinal("BillImageUrl");
                int BusinessCompanyId = r.GetOrdinal("BusinessCompanyId");
                int BusinessCompanyName = r.GetOrdinal("BusinessCompanyName");
                int IsStockIn = r.GetOrdinal("IsStockIn");
                int ConfirmRemark = r.GetOrdinal("ConfirmRemark");
                int TotalPrice = r.GetOrdinal("TotalPrice");
                int AccountPaid = r.GetOrdinal("AccountPaid");
                int GoodsId = r.GetOrdinal("GoodsId");
                int GoodsName = r.GetOrdinal("GoodsName");
                int EmployeeId = r.GetOrdinal("EmployeeId");
                int CheckStatus = r.GetOrdinal("CheckStatus");
                int CheckUser = r.GetOrdinal("CheckUser");
                int CheckDate = r.GetOrdinal("CheckDate");
                int CheckUserHistory = r.GetOrdinal("CheckUserHistory");
                int Remark = r.GetOrdinal("Remark");
                int Status = r.GetOrdinal("Status");
                int AddUser = r.GetOrdinal("AddUser");
                int AddTime = r.GetOrdinal("AddTime");
                int LastEditUser = r.GetOrdinal("LastEditUser");
                int LastEditTime = r.GetOrdinal("LastEditTime");
                G_PurchaseOrderInfo model = null;
                while (r.Read())
                {
                    model = new G_PurchaseOrderInfo();
                    model.PurchaseOrderId = r.GetInt32(PurchaseOrderId);
                    model.BillNo = r.GetString(BillNo);
                    model.BillStatus = r.GetInt32(BillStatus);
                    model.BillImageUrl = r.GetString(BillImageUrl);
                    model.BusinessCompanyId = r.GetInt32(BusinessCompanyId);
                    model.BusinessCompanyName = r.GetString(BusinessCompanyName);
                    model.IsStockIn = r.GetBoolean(IsStockIn);
                    model.ConfirmRemark = r.GetString(ConfirmRemark);
                    model.TotalPrice = r.GetDecimal(TotalPrice);
                    model.AccountPaid = r.GetDecimal(AccountPaid);
                    model.GoodsId = r.GetString(GoodsId);
                    model.GoodsName = r.GetString(GoodsName);
                    model.EmployeeId = r.GetInt32(EmployeeId);
                    model.CheckStatus = r.GetInt32(CheckStatus);
                    model.CheckUser = r.GetInt32(CheckUser);
                    model.CheckDate = r.GetDateTime(CheckDate);
                    model.CheckUserHistory = r.GetString(CheckUserHistory);
                    model.Remark = r.GetString(Remark);
                    model.Status = r.GetInt32(Status);
                    model.AddUser = r.GetInt32(AddUser);
                    model.AddTime = r.GetDateTime(AddTime);
                    model.LastEditUser = r.GetInt32(LastEditUser);
                    model.LastEditTime = r.GetDateTime(LastEditTime);
                    list.Add(model);
                }
                r.Close();
            }
            return list;
        }

        public int GetG_PurchaseOrderInfoCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat("SELECT COUNT(*) FROM G_PurchaseOrderInfo WITH(NOLOCK) WHERE [Status]=0 {0}", strWhere);
            return int.Parse(ExecuteScalar(ConfigHelper.ConnectionStrings, strSql.ToString()).ToString());
        }


        public G_PurchaseOrderInfo GetG_PurchaseOrderInfoModel(int purchaseOrderId)
        {
            G_PurchaseOrderInfo model = new G_PurchaseOrderInfo();
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT ");
            strSql.Append("PurchaseOrderId");
            strSql.Append(",BillNo");
            strSql.Append(",BillStatus");
            strSql.Append(",BillImageUrl");
            strSql.Append(",BusinessCompanyId");
            strSql.Append(",BusinessCompanyName");
            strSql.Append(",IsStockIn");
            strSql.Append(",ConfirmRemark");
            strSql.Append(",TotalPrice");
            strSql.Append(",AccountPaid");
            strSql.Append(",GoodsId");
            strSql.Append(",GoodsName");
            strSql.Append(",EmployeeId");
            strSql.Append(",CheckStatus");
            strSql.Append(",CheckUser");
            strSql.Append(",CheckDate");
            strSql.Append(",CheckUserHistory");
            strSql.Append(",Remark");
            strSql.Append(",Status");
            strSql.Append(",AddUser");
            strSql.Append(",AddTime");
            strSql.Append(",LastEditUser");
            strSql.Append(",LastEditTime");
            strSql.Append(" FROM G_PurchaseOrderInfo WITH(NOLOCK) WHERE PurchaseOrderId=@PurchaseOrderId");
            List<SqlParameter> pList = new List<SqlParameter>();
            pList.Add(new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = purchaseOrderId });
            using (SqlDataReader r = this.ExecuteReader(ConfigHelper.ConnectionStrings, strSql.ToString(), pList.ToArray()))
            {
                int PurchaseOrderId = r.GetOrdinal("PurchaseOrderId");
                int BillNo = r.GetOrdinal("BillNo");
                int BillStatus = r.GetOrdinal("BillStatus");
                int BillImageUrl = r.GetOrdinal("BillImageUrl");
                int BusinessCompanyId = r.GetOrdinal("BusinessCompanyId");
                int BusinessCompanyName = r.GetOrdinal("BusinessCompanyName");
                int IsStockIn = r.GetOrdinal("IsStockIn");
                int ConfirmRemark = r.GetOrdinal("ConfirmRemark");
                int TotalPrice = r.GetOrdinal("TotalPrice");
                int AccountPaid = r.GetOrdinal("AccountPaid");
                int GoodsId = r.GetOrdinal("GoodsId");
                int GoodsName = r.GetOrdinal("GoodsName");
                int EmployeeId = r.GetOrdinal("EmployeeId");
                int CheckStatus = r.GetOrdinal("CheckStatus");
                int CheckUser = r.GetOrdinal("CheckUser");
                int CheckDate = r.GetOrdinal("CheckDate");
                int CheckUserHistory = r.GetOrdinal("CheckUserHistory");
                int Remark = r.GetOrdinal("Remark");
                int Status = r.GetOrdinal("Status");
                int AddUser = r.GetOrdinal("AddUser");
                int AddTime = r.GetOrdinal("AddTime");
                int LastEditUser = r.GetOrdinal("LastEditUser");
                int LastEditTime = r.GetOrdinal("LastEditTime");
                if (r.Read())
                {
                    model.PurchaseOrderId = r.GetInt32(PurchaseOrderId);
                    model.BillNo = r.GetString(BillNo);
                    model.BillStatus = r.GetInt32(BillStatus);
                    model.BillImageUrl = r.GetString(BillImageUrl);
                    model.BusinessCompanyId = r.GetInt32(BusinessCompanyId);
                    model.BusinessCompanyName = r.GetString(BusinessCompanyName);
                    model.IsStockIn = r.GetBoolean(IsStockIn);
                    model.ConfirmRemark = r.GetString(ConfirmRemark);
                    model.TotalPrice = r.GetDecimal(TotalPrice);
                    model.AccountPaid = r.GetDecimal(AccountPaid);
                    model.GoodsId = r.GetString(GoodsId);
                    model.GoodsName = r.GetString(GoodsName);
                    model.EmployeeId = r.GetInt32(EmployeeId);
                    model.CheckStatus = r.GetInt32(CheckStatus);
                    model.CheckUser = r.GetInt32(CheckUser);
                    model.CheckDate = r.GetDateTime(CheckDate);
                    model.CheckUserHistory = r.GetString(CheckUserHistory);
                    model.Remark = r.GetString(Remark);
                    model.Status = r.GetInt32(Status);
                    model.AddUser = r.GetInt32(AddUser);
                    model.AddTime = r.GetDateTime(AddTime);
                    model.LastEditUser = r.GetInt32(LastEditUser);
                    model.LastEditTime = r.GetDateTime(LastEditTime);
                }
                r.Close();
            }
            return model;
        }




        /// <summary>
        /// 订单完成
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public bool EditG_PurchaseOrderInfo_BillStatus(G_PurchaseOrderInfo model)
        {
            string strSql = @"
UPDATE  dbo.G_PurchaseOrderInfo
SET     IsStockIn = 1 ,

        ConfirmRemark = @ConfirmRemark ,
        LastEditUser = @LastEditUser ,
        LastEditTime = @LastEditTime
WHERE   PurchaseOrderId = @PurchaseOrderId
        AND [Status] = 0
        AND CheckStatus = 2
        AND BillStatus = 1 ,";
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@PurchaseOrderId", SqlDbType.Int, 4) { Value = model.PurchaseOrderId },
                new SqlParameter("@ConfirmRemark", SqlDbType.NVarChar, 200) { Value = model.ConfirmRemark },
                new SqlParameter("@LastEditUser", SqlDbType.Int, 4) { Value = model.LastEditUser },
                new SqlParameter("@LastEditTime", SqlDbType.DateTime) { Value = model.LastEditTime }
            };
            return ExecuteNonQuery(ConfigHelper.ConnectionStrings, strSql, pList.ToArray()) > 0;
        }

        public List<G_PurchaseOrderInfo> GetG_PurchaseOrderInfoLikeFront10(string billNo)
        {
            List<G_PurchaseOrderInfo> list = new List<G_PurchaseOrderInfo>();
            string strSql = @"
SELECT  PurchaseOrderId ,
        BillNo
FROM    G_PurchaseOrderInfo WITH ( NOLOCK )
WHERE   [Status] = 0
        AND CheckStatus = 2
        AND BillStatus = 1
        AND BillNo LIKE '%'+@BillNo+'%'";
            List<SqlParameter> pList = new List<SqlParameter>
            {
                new SqlParameter("@BillNo", SqlDbType.NVarChar, 30) { Value = billNo }
            };
            using (SqlDataReader r = ExecuteReader(ConfigHelper.ConnectionStrings, strSql, pList.ToArray()))
            {
                int PurchaseOrderId = r.GetOrdinal("PurchaseOrderId");
                int BillNo = r.GetOrdinal("BillNo");

                G_PurchaseOrderInfo model = null;
                while (r.Read())
                {
                    model = new G_PurchaseOrderInfo
                    {
                        PurchaseOrderId = r.GetInt32(PurchaseOrderId),
                        BillNo = r.GetString(BillNo)
                    };
                    list.Add(model);
                }
                r.Close();
            }
            return list;
        }
    }
}
